package com.client.ryt.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.client.ryt.R;
import com.client.ryt.basic.BaseActivity;
import com.client.ryt.basic.BaseException;
import com.client.ryt.basic.BaseLiteners;
import com.client.ryt.basic.BaseResponse;
import com.client.ryt.basic.BaseTask;
import com.client.ryt.config.LoginConfig;
import com.client.ryt.entity.biz.StatementEntity;
import com.client.ryt.entity.biz.TradeRecordEntity;
import com.client.ryt.entity.parameter.StatementConfirmParamters;
import com.client.ryt.task.http.request.StatementConfirmRequest;

import java.sql.Statement;
import java.util.List;

/**
 * Created by heyao on 2014/12/10.
 * 对账清单适配器
 */
public class StatementAdapter  extends BaseAdapter{
    private Context context;
    private List<StatementEntity> list;

    public StatementAdapter(Context context, List<StatementEntity> list){
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
       final StatementEntity hh = list.get(position);
        H h = null;
        if(view==null){
            h = new H();
            view = LayoutInflater.from(context).inflate(R.layout.item_statement, parent, false);
            h.tv_statementitem = (TextView)view.findViewById(R.id.tv_item_statementitem);
            h.tv_statedate = (TextView)view.findViewById(R.id.tv_item_statementedate);
            h.tv_statetimes = (TextView)view.findViewById(R.id.tv_item_statementtimes);
            h.tv_statestate = (Button)view.findViewById(R.id.btn_item_statementstate);
            view.setTag(h);
        }else{
            h = (H)view.getTag();
        }
        h.tv_statementitem.setText(hh.getTitle());
        h.tv_statedate.setText(hh.getCreateTime());
        h.tv_statetimes.setText("已使用 "+String.valueOf(hh.getTimesUsed())+" 次");
        h.tv_statestate.setText(hh.getAuthStatusCn());


        if(hh.getAuthStatus() == 0){
            final StatementEntity se = hh;
            h.tv_statestate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(final View view) {

                    new AlertDialog.Builder(view.getContext())
                    .setIcon(android.R.drawable.ic_dialog_alert)
                    .setTitle("确认")
                    .setMessage("确定要执行此操作?")
                    .setPositiveButton("是", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            StatementConfirmParamters statementConfirmParamters = new StatementConfirmParamters();
                            statementConfirmParamters.setStatementid(Long.parseLong(se.getId()));
                            statementConfirmParamters.setToken(LoginConfig.getLoginConfig().getToken());
                            statementConfirmParamters.setUsername(LoginConfig.getLoginConfig().getShopMember().getUsername());
                            new BaseTask<StatementConfirmRequest, BaseResponse>((BaseActivity)context, StatementConfirmRequest.class, BaseResponse.class, statementConfirmParamters, new BaseLiteners.BaseResponseListener() {
                                @Override
                                public void OnFail(BaseException e) {
                                    ((BaseActivity)context).showToast(e.getErrmsg());
                                }

                                @Override
                                public void OnSuccess(BaseResponse o) {
                                    ((Button)view).setText("已确认");
                                }
                            }, false).execute();
                        }
                    })
                    .setNegativeButton("否", null)
                    .show();

                }
            });
        }


        return view;
    }

    class H{
        TextView tv_statementitem;
        TextView tv_statedate;
        TextView tv_statetimes;
        Button tv_statestate;
    }
}
